package com.blb.controller;


import com.blb.entity.Department;
import com.blb.entity.Post;
import com.blb.entity.Staff;
import com.blb.entity.User;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.IDepartmentService;
import com.blb.service.IPostService;
import com.blb.service.IStaffService;
import com.blb.service.IUserService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-15
 */
@Controller
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private IStaffService staffService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IPostService postService;
    @Autowired
    private IUserService userService;

    private static User user;

    /**
     * 得到登录用户对象
     * @return
     */
    public String getCompanyId(){
        //得到登录的用户对象
        User user = ShiroUtils.getUser();
        //得到公司id
        String cId=user.getCompanyId();
        return cId;
    }

    /**
     * 跳页面方法 权限认证
     * @param model
     * @param page
     * @return
     */
    @RequestMapping("{page}")
    @RequiresPermissions("staff")
    public String staff(Model model,@PathVariable String page){
        user=ShiroUtils.getUser();
        String cId = getCompanyId();
        //查询所有父级部门
        List<Department> departments = departmentService.getDepartmentsByPid("0",cId);
        model.addAttribute("departments",departments);
        return "personal/"+page;
    }

    /**
     * 员工管理分页和模糊查询
     * @param model
     * @param name
     * @param dId
     * @param start
     * @param length
     * @return
     */
    @ResponseBody
    @RequestMapping("page")
    public BlbPage page(Model model, @RequestParam(required = false, value = "name")  String name, @RequestParam(required = false, value = "dId") String dId, int start, int length){
        String cId = getCompanyId();
        //查询分页数据
        List<Staff> staff = staffService.getStaffs(cId,start,length,name,dId);
        int count= staffService.count(cId,name,dId);
       //判断是否为超级管理员或管理员登录
        if("1".equals(user.getId())||"0".equals(user.getPId())){
           int count1=count-1;
           return new DataTablePage(count1, count1, staff);
       }
        return new DataTablePage(count, count, staff);
    }

    /**
     * 验证
     * @return
     */

    @RequestMapping("verify")
    @ResponseBody
    public ResponseHelp verifyAll(@RequestBody(required = false) User user){

        User select =staffService.select(user.getUserTel(), user.getNo(),user.getUserName());
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(select);
        return responseHelp;
    }


    /**
     * 添加员工
     * @param
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public ResponseHelp add(@RequestBody(required = false) User user,HttpSession session){
        //如果未为员工分配部门，则把员工里的did设为-1
        if(user.getDId()==""){
            user.setDId("-1");
        }
        String cId = getCompanyId();
        user.setCompanyId(cId);
        user.setId(UUID.randomUUID().toString().replace("-", ""));
        String salt = UUIDUtils.getId();
        user.setSalt(salt);
        //给密码加密
        String pas = ShiroUtils.enPas(user.getPassWord(), salt);
        user.setPassWord(pas);
        userService.save(user);

        ResponseHelp responseHelp = new ResponseHelp();

        return responseHelp;
    }

    /**
     * 得到所有父部门
     * @return
     */
    @ResponseBody
    @RequestMapping("parentDepartments")
    public ResponseHelp parentDepartments(){
        /*System.out.println(1/0);*/
        String cId = getCompanyId();
        List<Department> departments = departmentService.getDepartmentsByPid("0",cId);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(departments);
        return responseHelp;
    }

    /**
     * 通过父部门找到分部门
     * @param dId
     * @return
     */
    @ResponseBody
    @RequestMapping("sonDepartments")
    public ResponseHelp sonDepartments(String dId){
        //查询分部门
        List<Department> departments = departmentService.getDepartmentsById(dId);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(departments);
        return responseHelp;
    }

    /**
     * 通过分部门找到职位
     * @param dId
     * @return
     */
    @ResponseBody
    @RequestMapping("posts")
    public ResponseHelp posts(String dId){
        List<Post> posts= postService.getPostsByDId(dId);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(posts);
        return responseHelp;
    }


    /**
     * 通过员工id找到员工和部门和职位信息
     * @param id
     * @return
     */

    @RequestMapping("find")
    @ResponseBody
    public ResponseHelp findStaffById(String id){
       Map<String, Object> map = staffService.selectAllById(id);
       ResponseHelp responseHelp = new ResponseHelp();
       responseHelp.setData(map);
       return responseHelp;
    }

    /**
     * 通过员工id修改员工信息
     * @param 
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public ResponseHelp update(@RequestBody User user){
        userService.updateById(user);
        ResponseHelp responseHelp = new ResponseHelp();
        return responseHelp;
    }


    /**
     * 修改员工账号状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("updateStatus")
    @ResponseBody
    public ResponseHelp updateStatus(String id,String status){
        staffService.updateStatus(id, status);
        ResponseHelp responseHelp = new ResponseHelp();
        return responseHelp;

    }

    /**
     * 批量修改员工状态
     * @param ids
     * @return
     */
    @RequestMapping("updateAllStatus")
    @ResponseBody
    public ResponseHelp updateAllStatus(@RequestBody(required = false) String[] ids){
        staffService.updateAllStatus(ids);
        ResponseHelp responseHelp = new ResponseHelp();
        return responseHelp;

    }

}

